package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.entity.Setmeal;
import com.itheima.dto.SetmealDTO;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: fan
 * @Desc: 套餐模块
 * @Date: 2022/6/5 11:07
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 功能描述: 套餐分页查询
     *
     * @return : com.itheima.common.Result
     */
    @GetMapping("/page")
    public PageResult page(@RequestParam("page") Integer page,
                           @RequestParam("pageSize") Integer pageSize,
                           @RequestParam(value = "name", required = false) String name) {
        Page<Setmeal> setmealPage = setmealService.page(page, pageSize, name);
        if (setmealPage == null) {
            return new PageResult(Code.ERROR, MessageConstant.SETMEAL_GET_ERROR);
        }
        List<Setmeal> records = setmealPage.getRecords();
        long total = setmealPage.getTotal();
        return new PageResult(Code.OK, MessageConstant.SETMEAL_GET_SUCCESS, records, total);
    }

    /**
     * 功能描述: 修改套餐时数据回显
     *
     * @return : com.itheima.common.Result
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id) {
        SetmealDTO setmealDTO = setmealService.findById(id);
        if (setmealDTO == null) {
            return new Result(Code.ERROR, MessageConstant.SETMEAL_GET_ERROR);
        }
        return new Result(Code.OK, MessageConstant.SETMEAL_GET_SUCCESS, setmealDTO);
    }

    /**
     * 功能描述: 修改套餐功能
     *
     * @param setmealDTO
     * @return : com.itheima.common.Result
     */
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {

        int row = setmealService.updateSetmeal(setmealDTO);
        if (row == 0) {
            return new Result(Code.ERROR, MessageConstant.SETMEAL_UPDATE_ERROR);
        }
        return new Result(Code.OK, MessageConstant.SETMEAL_SAVE_SUCCESS);
    }

    /**
     * 功能描述: 新增套餐功能
     *
     * @return : com.itheima.common.Result
     */
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        int row = setmealService.addSetmeal(setmealDTO);
        if (row == 0) {
            return new Result(Code.ERROR, MessageConstant.SETMEAL_SAVE_ERROR);
        }
        return new Result(Code.OK, MessageConstant.SETMEAL_SAVE_SUCCESS);
    }

    /**
     * 功能描述: 套餐停售起售
     *
     * @param status
     * @param ids
     * @return : com.itheima.common.Result
     */
    @PostMapping("/status/{status}")
    public Result updeupdateSetmealStatus(@PathVariable Integer status, @RequestParam Long[] ids) {

        int row = setmealService.updeupdateSetmealStatus(status, ids);
        if (row == 0) {
            return new Result(Code.ERROR, MessageConstant.SETMEAL_UPDATE_ERROR);
        }
        return new Result(Code.OK, MessageConstant.SETMEAL_UPDATE_SUCCESS);
    }
    /**
     * 功能描述: 逻辑删除,批量删除
     * @param ids
     * @return : com.itheima.common.Result
     */
    @DeleteMapping
    public Result deleteSetmeal(@RequestParam("ids") Long[] ids){
        int row = setmealService.deleteSetmeal(ids);
        if (row == 0) {
            return new Result(Code.ERROR, MessageConstant.SETMEAL_DELETE_ERROR);
        }
        return new Result(Code.OK, MessageConstant.SETMEAL_DELETE_SUCCESS);
    }
}
